(function(){

    var Consum = Models.Form.extend({
        nullValue: '-',
        consumInputSelector: '.valoare-consum',

        _initTabs: function()
        {
            var selector = '.blocuri-tabs, .scari-tabs';
            var $tabs = $( selector, this.getElement() );
            $tabs.each(function(){
                new Models.Tabs( $(this) );
            })

        },

        // update field values
        _onSuccess: function( responseData )
        {
            var self = this;

            $.each(responseData, function(){
                var apId = this.apartament_id,
                    crId = this.criteriu_id,
                    value = this.valoare;

                var $field = self.getField([apId, crId]);
                $field.attr({
                    'value':    value,
                    'data-orig-value': value
                })
            })
        },

        /**
         * Send data only for fields that has numeric values and were changed
         *
         */
        getDataFields: function()
        {
            var self = this;
            var $fields = this._super();

            $fields.filter(this.consumInputSelector).each(function(){
                var $field = $(this),
                    value = $.trim( $field.val() ),
                    originalValue = $field.data('orig-value');

                if ( !value.length
                   || ( value != self.nullValue && !$.isNumeric(value) )
                   || value == originalValue )
               {
                   $fields = $fields.not( $field );
               }
            })

            return $fields;
        }


    });

    ////////////////////////////////////////////////////////////////////////////
    // attach the defined models to Models global container ////////////////////
    ////////////////////////////////////////////////////////////////////////////

    Models.Consumuri = {
        Add: Consum
    }

})()


